﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DeliveryService
{
    public class BLOrder
    {
        /// <summary>
        /// 提交新的菜品/商品订单
        /// OrderForm
        /// OrderForm_History
        /// OrderForm_Maker 需要一默认员工 : admin
        /// OrderForm_Foods
        /// OrderForm_Goods
        /// </summary>
        /// <param name="studentId">学生id</param>
        /// <param name="addressId">地址id</param>
        /// <param name="TotalAmount">商品总金额</param>
        /// <param name="privilegeAmount">优惠金额</param>
        /// <param name="payableAmount">应付金额</param>
        /// <param name="comment">备注</param>
        /// <param name="foodsIdAndAmount">菜品id和对应的数量,需要按照以下格式传输,如:
        /// [{"Id":1,"Amount":2},{"Id":2,"Amount":1},{"Id":3,"Amount":3}]</param>
        /// <param name="goodsIdAndAmount">商品id和对应的数量,需要按照以下格式传输,如:
        /// [{"Id":1,"Amount":2},{"Id":2,"Amount":1},{"Id":3,"Amount":3}]</param>
        /// <returns>订单ID</returns>
        public string AddNewOrder(int studentId, int addressId, double TotalAmount, double privilegeAmount,
                                    double payableAmount, string comment, string foodsIdAndAmount, string goodsIdAndAmount)
        {
            try
            {
                using (DeliveryDBDataContext db = new DeliveryDBDataContext())
                {
                    DateTime dt = DateTime.Now;
                    //add order
                    var order = new OrderForm
                    {
                        Address = db.Addresses.Single(a => a.Address_Id == addressId),
                        Student = db.Students.Single(s => s.Students_Id == studentId),
                        Amount = (decimal)TotalAmount,
                        Privilege_Amount = (decimal)privilegeAmount,
                        Payable_Amount = (decimal)payableAmount,
                        Comment = comment,
                        Status = (int)OrderStatus.waitingShipped,
                        OrderFormNumber = BaseClass.GenerateOrderNumber(),
                        IsReservation = false,
                        CreateTime = dt
                    };
                    db.OrderForms.InsertOnSubmit(order);
                    db.SubmitChanges();
                    //add history
                    var history = new OrderForm_History
                    {
                        OrderForm = order,
                        OrderForm_Maker = db.OrderForm_Makers.OrderBy(m => m.OrderForm_Staff_Id).FirstOrDefault(),
                        OrderStatus = (int)OrderStatus.waitingShipped,
                        SubmitTime = dt,
                        Comment = "新订单,待发货.",
                    };
                    db.OrderForm_Histories.InsertOnSubmit(history);
                    db.SubmitChanges();
                    // add foods order
                    var foods = JArray.Parse(foodsIdAndAmount).Select(x => new OrderForm_Food
                    {
                        Food = db.Foods.Single(f => f.Foods_Id == int.Parse(x["Id"].ToString())),
                        Amount = int.Parse(x["Amount"].ToString()),
                        OrderForm = order
                    });
                    db.OrderForm_Foods.InsertAllOnSubmit(foods);
                    db.SubmitChanges();
                    //add goods order
                    var goods = JArray.Parse(goodsIdAndAmount).Select(x => new OrderForm_Good
                    {
                        Good = db.Goods.Single(g => g.Goods_Id == int.Parse(x["Id"].ToString())),
                        Amount = int.Parse(x["Amount"].ToString()),
                        OrderForm = order
                    });
                    db.OrderForm_Goods.InsertAllOnSubmit(goods);
                    db.SubmitChanges();
                    return order.OrderFormNumber;
                }
            }
            catch (Exception ex)
            {
                throw;
                // return -1;
            }
        }


        /// <summary>
        /// 更改订单状态
        /// </summary>
        /// <param name="OrderFormId">订单ID</param>
        /// <param name="status">0: 待发货, 1: 已发货  2:已收货  3:已取消</param>
        /// <param name="staffId">操作订单的员工ID</param>
        /// <param name="comment">备注/param>
        /// <returns></returns>
        public bool ChangeOrderFormStatus(int OrderFormId, int status, int staffId, string comment)
        {
            try
            {
                using (DeliveryDBDataContext db = new DeliveryDBDataContext())
                {
                    var order = db.OrderForms.Single(o => o.OrderForm_Id == OrderFormId);
                    order.Status = status;
                    var history = new OrderForm_History
                    {
                        OrderForm = order,
                        OrderForm_Maker = db.OrderForm_Makers.Single(x => x.OrderForm_Staff_Id == staffId),
                        OrderStatus = status,
                        SubmitTime = DateTime.Now,
                        Comment = comment
                    };
                    db.OrderForm_Histories.InsertOnSubmit(history);
                    db.SubmitChanges();
                    return true;
                }

            }
            catch (Exception)
            {
                return false;
            }
        }


        /// <summary>
        /// 根据学生姓名获取此学生的订单列表
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <returns>订单详情</returns>
        public string GetOrderFormsByStudentId(int studentId)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.OrderForms.Where(o => o.Student.Students_Id == studentId)
                              .OrderByDescending(o => o.CreateTime)
                              .Select(o => new
                              {
                                  OrderFormId = o.OrderForm_Id,
                                  Address = o.Address.Address_Info,
                                  Comment = o.Comment,
                                  PayableAmount = o.Payable_Amount.HasValue ? (double)o.Payable_Amount.Value : 0,
                                  PrivilegeAmount = o.Privilege_Amount.HasValue ? (double)o.Privilege_Amount.Value : 0,
                                  TotalAmount = o.Amount.HasValue ? (double)o.Amount.Value : 0,
                                  Status = o.Status,
                                  OrderFormNumber = o.OrderFormNumber,
                                  CreateTime = o.CreateTime,

                                  FoodPictUrls = o.OrderForm_Foods.Select(g => new
                                  {
                                      url = g.Food.Foods_Pic
                                  }).Take(3),

                                  GoodPictUrl = o.OrderForm_Goods.Select(g => new
                                  {
                                      url = g.Good.Goods_Pic
                                  }).Take(3)
                              });
                return JsonConvert.SerializeObject(query);
            }
        }


        /// <summary>
        /// 根据学生ID和订单状态查询订单
        /// </summary>
        /// <param name="studentId">学生ID</param>
        /// <param name="orderStatus">订单状态</param>
        /// <returns>订单详情</returns>
        public string GetOrderFormsByStatues(int studentId, int orderStatus)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.OrderForms.Where(o => o.Student.Students_Id == studentId && o.Status == orderStatus)
                              .OrderByDescending(o => o.CreateTime)
                              .Select(o => new
                              {
                                  OrderFormId = o.OrderForm_Id,
                                  Address = o.Address.Address_Info,
                                  Comment = o.Comment,
                                  PayableAmount = o.Payable_Amount.HasValue ? (double)o.Payable_Amount.Value : 0,
                                  PrivilegeAmount = o.Privilege_Amount.HasValue ? (double)o.Privilege_Amount.Value : 0,
                                  TotalAmount = o.Amount.HasValue ? (double)o.Amount.Value : 0,
                                  Status = o.Status,
                                  OrderFormNumber = o.OrderFormNumber,
                                  CreateTime = o.CreateTime,
                                  FoodPictUrls = o.OrderForm_Foods.Select(g => new
                                  {
                                      url = g.Food.Foods_Pic
                                  }).Take(3),

                                  GoodPictUrl = o.OrderForm_Goods.Select(g => new
                                  {
                                      url = g.Good.Goods_Pic
                                  }).Take(3)

                                  //FoodsList = o.OrderForm_Foods.Select(g => new
                                  //{
                                  //    GoodsId = g.Food.Foods_Id,
                                  //    Amount = g.Amount,
                                  //    FoodPicture = g.Food.Foods_Pic
                                  //})
                                  //GoodsList = o.OrderForm_Goods.Select(g => new
                                  //{
                                  //    GoodsId = g.Good.Goods_Id,
                                  //    Amount = g.Amount,
                                  //    GoodPicture = g.Good.Goods_Pic
                                  //})
                              });
                return JsonConvert.SerializeObject(query);
            }
        }

        /// <summary>
        /// 根据订单号获取该订单对应的菜品对象和商品数量
        /// </summary>
        /// <param name="orderFormNumber">订单号</param>
        /// <returns>菜品订单详情</returns>
        public string GetOrderFormsByOrderFormNumber(string orderFormNumber)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.OrderForms.Where(o => o.OrderFormNumber == orderFormNumber)
                              .OrderByDescending(o => o.CreateTime)
                              .Select(o => new
                              {
                                  // 由于首先操作GetOrderFormsByStudentId才会操作此接口，而订单详情已经通过它获得，顾删去
                                  //OrderFormId = o.OrderForm_Id,
                                  //Address = o.Address.Address_Info,
                                  //Comment = o.Comment,
                                  //PayableAmount = o.Payable_Amount.HasValue ? (double)o.Payable_Amount.Value : 0,
                                  //PrivilegeAmount = o.Privilege_Amount.HasValue ? (double)o.Privilege_Amount.Value : 0,
                                  //TotalAmount = o.Amount.HasValue ? (double)o.Amount.Value : 0,
                                  //Status = o.Status,
                                  //CreateTime = o.CreateTime,
                                  //OrderFormNumber = o.OrderFormNumber,
                                  FoodsList = o.OrderForm_Foods.Select(g => new
                                  {
                                      FoodsId = g.Food.Foods_Id,
                                      Amount = g.Amount,
                                      FoodPicture = g.Food.Foods_Pic,
                                      Name = g.Food.Foods_Name,
                                      Price = g.Food.Foods_Price,
                                      Category = g.Food.FoodsCategory.FoodsCategory_Name,
                                      Taste = g.Food.FoodsTaste.FoodsTaste_Name,
                                      RestaurantName = g.Food.Restaurant.Restaurant_Name,
                                      RestaurantIsBusy = g.Food.Restaurant.IsBusy,
                                  }),
                                  GoodsList = o.OrderForm_Goods.Select(g => new
                                  {
                                      GoodsId = g.Good.Goods_Id,
                                      Amount = g.Amount,
                                      GoodPicture = g.Good.Goods_Pic,
                                      GoodBrand = g.Good.Brand.Brand_Name,
                                      GoodsName = g.Good.Goods_Name,
                                      Price = g.Good.Goods_Price,
                                      Standard = g.Good.Goods_Standard,
                                      CategoryName = g.Good.Goodscategory_.GoodsCategory.GoodsCategory_Name,
                                      CategorySmallName = g.Good.Goodscategory_.GoodsCategory_S_Name,
                                      IsSpecialGoods = g.Good.IsSpecialGoods,
                                      Sales = g.Good.Sales
                                  })
                                  //FoodsList = o.OrderForm_Foods.Select(g => new
                                  //{
                                  //    GoodsId = g.Food.Foods_Id,
                                  //    Amount = g.Amount,
                                  //    FoodPicture = g.Food.Foods_Pic
                                  //}),
                                  //GoodsList = o.OrderForm_Goods.Select(g => new
                                  //{
                                  //    GoodsId = g.Good.Goods_Id,
                                  //    Amount = g.Amount,
                                  //    GoodPicture = g.Good.Goods_Pic
                                  //})
                              });
                return JsonConvert.SerializeObject(query);
            }
        }

        /// <summary>
        /// 根据订单状态查询订单
        /// </summary>
        /// <param name="orderStatus">订单状态</param>
        /// <returns>订单详情</returns>
        public string GetOrderFormsByStatues(int orderStatus)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.OrderForms.Where(o => o.Status == orderStatus)
                              .OrderByDescending(o => o.CreateTime)
                              .Select(o => new
                              {
                                  OrderFormId = o.OrderForm_Id,
                                  Address = o.Address.Address_Info,
                                  //Comment = o.Comment,
                                  //PayableAmount = o.Payable_Amount.HasValue ? (double)o.Payable_Amount.Value : 0,
                                  //PrivilegeAmount = o.Privilege_Amount.HasValue ? (double)o.Privilege_Amount.Value : 0,
                                  TotalAmount = o.Amount.HasValue ? (double)o.Amount.Value : 0,
                                  Status = Enum.GetName(typeof(OrderStatus), o.Status),
                                  OrderFormNumber = o.OrderFormNumber,
                                  CreateTime = o.CreateTime,
                                  FoodPictUrls = o.OrderForm_Foods.Select(g => new
                                  {
                                      url = g.Food.Foods_Pic
                                  }).Take(3),

                                  GoodPictUrl = o.OrderForm_Goods.Select(g => new
                                  {
                                      url = g.Good.Goods_Pic
                                  }).Take(3)
                                  //FoodsList = o.OrderForm_Foods.Select(g => new
                                  //{
                                  //    GoodsId = g.Food.Foods_Id,
                                  //    Amount = g.Amount,
                                  //    FoodPicture = g.Food.Foods_Pic
                                  //}),
                                  //GoodsList = o.OrderForm_Goods.Select(g => new
                                  //{
                                  //    GoodsId = g.Good.Goods_Id,
                                  //    Amount = g.Amount,
                                  //    GoodPicture = g.Good.Goods_Pic
                                  //})
                              });
                return JsonConvert.SerializeObject(query);
            }
        }

        /// <summary>
        /// 根据订单状态查询订单,并返回详情。此接口仅供电脑服务端使用
        /// </summary>
        /// <param name="orderStatus">订单状态</param>
        /// <returns>订单详情</returns>
        public string GetOrderFormsDetailsByStatues(int orderStatus)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.OrderForms.Where(o => o.Status == orderStatus)
                              .OrderByDescending(o => o.CreateTime)
                              .Select(o => new
                              {
                                  StudentId = o.StudentId,
                                  StudentName = o.Student.name,
                                  PhoneNumber = o.Student.phoneNumber,
                                  recipientAdress = o.Address.recipient,
                                  recipientPhone = o.Address.phoneNum,

                                  OrderFormId = o.OrderForm_Id,
                                  Address = o.Address.Address_Info,
                                  Comment = o.Comment,
                                  PayableAmount = o.Payable_Amount.HasValue ? (double)o.Payable_Amount.Value : 0,
                                  PrivilegeAmount = o.Privilege_Amount.HasValue ? (double)o.Privilege_Amount.Value : 0,
                                  TotalAmount = o.Amount.HasValue ? (double)o.Amount.Value : 0,
                                  Status = Enum.GetName(typeof(OrderStatus), o.Status),
                                  OrderFormNumber = o.OrderFormNumber,
                                  CreateTime = o.CreateTime,
                                  FoodsList = o.OrderForm_Foods.Select(g => new
                                  {
                                      FoodsId = g.Food.Foods_Id,
                                      Amount = g.Amount,
                                      FoodPicture = g.Food.Foods_Pic,
                                      Name = g.Food.Foods_Name,
                                      Price = g.Food.Foods_Price,
                                      Category = g.Food.FoodsCategory.FoodsCategory_Name,
                                      Taste = g.Food.FoodsTaste.FoodsTaste_Name,
                                      RestaurantName = g.Food.Restaurant.Restaurant_Name,
                                      RestaurantPhone = g.Food.Restaurant.Restaurant_Phone,
                                      RestaurantIsBusy = g.Food.Restaurant.IsBusy,
                                  }),
                                  GoodsList = o.OrderForm_Goods.Select(g => new
                                  {
                                      GoodsId = g.Good.Goods_Id,
                                      Amount = g.Amount,
                                      GoodPicture = g.Good.Goods_Pic,
                                      GoodBrand = g.Good.Brand.Brand_Name,
                                      GoodsName = g.Good.Goods_Name,
                                      Price = g.Good.Goods_Price,
                                      Standard = g.Good.Goods_Standard,
                                      CategoryName = g.Good.Goodscategory_.GoodsCategory.GoodsCategory_Name,
                                      CategorySmallName = g.Good.Goodscategory_.GoodsCategory_S_Name,
                                      IsSpecialGoods = g.Good.IsSpecialGoods,
                                      Sales = g.Good.Sales
                                  })
                              });
                return JsonConvert.SerializeObject(query);
            }
        }

        /// <summary>
        /// 根据下单时间查询订单,并返回详情。此接口仅供电脑服务端使用
        /// </summary>
        /// <param name="QureyDateTime">下单时间</param>
        /// <returns></returns>             
        public string GetOrderFormsDetailsByData(DateTime QureyDateTime)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.OrderForms.Where(o => ((o.CreateTime.Day == QureyDateTime.Day) && (o.CreateTime.Month == QureyDateTime.Month) && (o.CreateTime.Year == QureyDateTime.Year)))
                              .OrderByDescending(o => o.CreateTime)
                    // 以下都是从上一个方法copy的
                              .Select(o => new
                              {
                                  StudentId = o.StudentId,
                                  StudentName = o.Student.name,
                                  PhoneNumber = o.Student.phoneNumber,
                                  recipientAdress = o.Address.recipient,
                                  recipientPhone = o.Address.phoneNum,

                                  OrderFormId = o.OrderForm_Id,
                                  Address = o.Address.Address_Info,
                                  Comment = o.Comment,
                                  PayableAmount = o.Payable_Amount.HasValue ? (double)o.Payable_Amount.Value : 0,
                                  PrivilegeAmount = o.Privilege_Amount.HasValue ? (double)o.Privilege_Amount.Value : 0,
                                  TotalAmount = o.Amount.HasValue ? (double)o.Amount.Value : 0,
                                  Status = Enum.GetName(typeof(OrderStatus), o.Status),
                                  OrderFormNumber = o.OrderFormNumber,
                                  CreateTime = o.CreateTime,
                                  FoodsList = o.OrderForm_Foods.Select(g => new
                                  {
                                      FoodsId = g.Food.Foods_Id,
                                      Amount = g.Amount,
                                      FoodPicture = g.Food.Foods_Pic,
                                      Name = g.Food.Foods_Name,
                                      Price = g.Food.Foods_Price,
                                      Category = g.Food.FoodsCategory.FoodsCategory_Name,
                                      Taste = g.Food.FoodsTaste.FoodsTaste_Name,
                                      RestaurantName = g.Food.Restaurant.Restaurant_Name,
                                      RestaurantPhone = g.Food.Restaurant.Restaurant_Phone,
                                      RestaurantIsBusy = g.Food.Restaurant.IsBusy,
                                  }),
                                  GoodsList = o.OrderForm_Goods.Select(g => new
                                  {
                                      GoodsId = g.Good.Goods_Id,
                                      Amount = g.Amount,
                                      GoodPicture = g.Good.Goods_Pic,
                                      GoodBrand = g.Good.Brand.Brand_Name,
                                      GoodsName = g.Good.Goods_Name,
                                      Price = g.Good.Goods_Price,
                                      Standard = g.Good.Goods_Standard,
                                      CategoryName = g.Good.Goodscategory_.GoodsCategory.GoodsCategory_Name,
                                      CategorySmallName = g.Good.Goodscategory_.GoodsCategory_S_Name,
                                      IsSpecialGoods = g.Good.IsSpecialGoods,
                                      Sales = g.Good.Sales
                                  })
                              });
                return JsonConvert.SerializeObject(query);
            }
        }

        /// <summary>
        /// 按时间段查询订单
        /// </summary>
        /// <param name="FromTime">开始时间</param>
        /// <param name="ToTime">结束时间</param>
        /// <returns>该时间段内的订单详情</returns>
        public string GetOrderFormsDetailsByDurationTime(DateTime FromTime, DateTime ToTime)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.OrderForms.Where(o => ((DateTime.Compare(FromTime, o.CreateTime) <= 0) && (DateTime.Compare(ToTime, o.CreateTime) >= 0)))
                              .OrderByDescending(o => o.CreateTime)
                    // 以下都是从上一个方法copy的
                              .Select(o => new
                              {
                                  StudentId = o.StudentId,
                                  StudentName = o.Student.name,
                                  PhoneNumber = o.Student.phoneNumber,
                                  recipientAdress = o.Address.recipient,
                                  recipientPhone = o.Address.phoneNum,

                                  OrderFormId = o.OrderForm_Id,
                                  Address = o.Address.Address_Info,
                                  Comment = o.Comment,
                                  PayableAmount = o.Payable_Amount.HasValue ? (double)o.Payable_Amount.Value : 0,
                                  PrivilegeAmount = o.Privilege_Amount.HasValue ? (double)o.Privilege_Amount.Value : 0,
                                  TotalAmount = o.Amount.HasValue ? (double)o.Amount.Value : 0,
                                  Status = Enum.GetName(typeof(OrderStatus), o.Status),
                                  OrderFormNumber = o.OrderFormNumber,
                                  CreateTime = o.CreateTime,
                                  FoodsList = o.OrderForm_Foods.Select(g => new
                                  {
                                      FoodsId = g.Food.Foods_Id,
                                      Amount = g.Amount,
                                      FoodPicture = g.Food.Foods_Pic,
                                      Name = g.Food.Foods_Name,
                                      Price = g.Food.Foods_Price,
                                      Category = g.Food.FoodsCategory.FoodsCategory_Name,
                                      Taste = g.Food.FoodsTaste.FoodsTaste_Name,
                                      RestaurantName = g.Food.Restaurant.Restaurant_Name,
                                      RestaurantPhone = g.Food.Restaurant.Restaurant_Phone,
                                      RestaurantIsBusy = g.Food.Restaurant.IsBusy,
                                  }),
                                  GoodsList = o.OrderForm_Goods.Select(g => new
                                  {
                                      GoodsId = g.Good.Goods_Id,
                                      Amount = g.Amount,
                                      GoodPicture = g.Good.Goods_Pic,
                                      GoodBrand = g.Good.Brand.Brand_Name,
                                      GoodsName = g.Good.Goods_Name,
                                      Price = g.Good.Goods_Price,
                                      Standard = g.Good.Goods_Standard,
                                      CategoryName = g.Good.Goodscategory_.GoodsCategory.GoodsCategory_Name,
                                      CategorySmallName = g.Good.Goodscategory_.GoodsCategory_S_Name,
                                      IsSpecialGoods = g.Good.IsSpecialGoods,
                                      Sales = g.Good.Sales
                                  })
                              });
                return JsonConvert.SerializeObject(query);
            }
        }

        /// <summary>
        /// 根据下单时间和订单状态查询订单,并返回详情。此接口仅供电脑服务端使用
        /// </summary>
        /// <param name="QureyDateTime">下单时间</param>
        /// <param name="orderStatus">订单状态</param>
        /// <returns></returns>             
        public string GetOrderFormsDetailsByDataAndStatues(DateTime QureyDateTime, int orderStatus)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.OrderForms.Where(o => ((o.Status == orderStatus) && (o.CreateTime.Day == QureyDateTime.Day) && (o.CreateTime.Month == QureyDateTime.Month) && (o.CreateTime.Year == QureyDateTime.Year)))
                              .OrderByDescending(o => o.CreateTime)
                    // 以下都是从上一个方法copy的
                              .Select(o => new
                              {
                                  StudentId = o.StudentId,
                                  StudentName = o.Student.name,
                                  PhoneNumber = o.Student.phoneNumber,
                                  recipientAdress = o.Address.recipient,
                                  recipientPhone = o.Address.phoneNum,

                                  OrderFormId = o.OrderForm_Id,
                                  Address = o.Address.Address_Info,
                                  Comment = o.Comment,
                                  PayableAmount = o.Payable_Amount.HasValue ? (double)o.Payable_Amount.Value : 0,
                                  PrivilegeAmount = o.Privilege_Amount.HasValue ? (double)o.Privilege_Amount.Value : 0,
                                  TotalAmount = o.Amount.HasValue ? (double)o.Amount.Value : 0,
                                  Status = Enum.GetName(typeof(OrderStatus), o.Status),
                                  OrderFormNumber = o.OrderFormNumber,
                                  CreateTime = o.CreateTime,
                                  FoodsList = o.OrderForm_Foods.Select(g => new
                                  {
                                      FoodsId = g.Food.Foods_Id,
                                      Amount = g.Amount,
                                      FoodPicture = g.Food.Foods_Pic,
                                      Name = g.Food.Foods_Name,
                                      Price = g.Food.Foods_Price,
                                      Category = g.Food.FoodsCategory.FoodsCategory_Name,
                                      Taste = g.Food.FoodsTaste.FoodsTaste_Name,
                                      RestaurantName = g.Food.Restaurant.Restaurant_Name,
                                      RestaurantPhone = g.Food.Restaurant.Restaurant_Phone,
                                      RestaurantIsBusy = g.Food.Restaurant.IsBusy,
                                  }),
                                  GoodsList = o.OrderForm_Goods.Select(g => new
                                  {
                                      GoodsId = g.Good.Goods_Id,
                                      Amount = g.Amount,
                                      GoodPicture = g.Good.Goods_Pic,
                                      GoodBrand = g.Good.Brand.Brand_Name,
                                      GoodsName = g.Good.Goods_Name,
                                      Price = g.Good.Goods_Price,
                                      Standard = g.Good.Goods_Standard,
                                      CategoryName = g.Good.Goodscategory_.GoodsCategory.GoodsCategory_Name,
                                      CategorySmallName = g.Good.Goodscategory_.GoodsCategory_S_Name,
                                      IsSpecialGoods = g.Good.IsSpecialGoods,
                                      Sales = g.Good.Sales
                                  })
                              });
                return JsonConvert.SerializeObject(query);
            }
        }

        /// <summary>
        /// 根据日期查询某一天的订单数
        /// </summary>
        /// <param name="QureyDateTime">需要查询的日期(年月日)</param>
        /// <returns></returns>
        public int GetOrdersCountByDate(DateTime QureyDateTime)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.OrderForms.Where(o => (o.CreateTime.Day == QureyDateTime.Day) && (o.CreateTime.Month == QureyDateTime.Month) && (o.CreateTime.Year == QureyDateTime.Year));
                return query.Count();
            }
        }

        /// <summary>
        /// 获取某一天第一个订单的ID，作为某一天订单的序号的基数
        /// </summary>
        /// <returns></returns>
        public int GetOneDaysFirstOrderId(DateTime QureyDateTime)
        {
            using (DeliveryDBDataContext db = new DeliveryDBDataContext())
            {
                var query = db.OrderForms.Where(o => (o.CreateTime.Day == QureyDateTime.Day) && (o.CreateTime.Month == QureyDateTime.Month) && (o.CreateTime.Year == QureyDateTime.Year))
                    .OrderBy(o => o.CreateTime);
                return query.First().OrderForm_Id;
            }
        }
    }
}